package com.lzh.dlykserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.constant.Constants;
import com.lzh.dlykserver.entity.TActivityRemark;
import com.lzh.dlykserver.mapper.TActivityRemarkMapper;
import com.lzh.dlykserver.query.ActivityRemarkQuery;
import com.lzh.dlykserver.service.ActivityRemarkService;
import com.lzh.dlykserver.util.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ActivityRemarkServiceImpl implements ActivityRemarkService {

    @Resource
    private TActivityRemarkMapper tActivityRemarkMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveaActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();

        //把activityRemarkQuery中的属性复制到tActivityRemark中
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);

        //创建人的ID
        Integer createById = JWTUtils.parseUserFromJWT(activityRemarkQuery.getToken()).getId();

        tActivityRemark.setCreateBy(createById);
        tActivityRemark.setCreateTime(new Date());

        return tActivityRemarkMapper.insertSelective(tActivityRemark);
    }

    @Override
    public PageInfo<TActivityRemark> getActivityRemarkByPage(Integer current, ActivityRemarkQuery activityRemarkQuery) {
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        List<TActivityRemark> list = tActivityRemarkMapper.selectActivityRemarkByPage(activityRemarkQuery);
        PageInfo<TActivityRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public TActivityRemark getActivityRemarkById(Integer id) {
        return tActivityRemarkMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateaActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();

        //把activityRemarkQuery中的属性复制到tActivityRemark中
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);

        //编辑人的ID
        Integer editById = JWTUtils.parseUserFromJWT(activityRemarkQuery.getToken()).getId();

        tActivityRemark.setEditBy(editById);
        tActivityRemark.setEditTime(new Date());

        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteActivityRemarkById(Integer id) {
        //逻辑删除:不删除数据，修改数据状态
        TActivityRemark tActivityRemark = new TActivityRemark();
        tActivityRemark.setId(id);
        tActivityRemark.setDeleted(1);
        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }
}
